<template>
  <!-- 水稳---统计报表----原材料消耗 -->
  <div class="department" id="department">
    <!-- 右侧 -->
    <div class="department_right">
      <div class="mechanism_nav app-container-search">
        <el-form ref="form" :model="getList" label-width="110px">
          <el-row :gutter="20">
            <el-col :span="6"
              ><el-form-item label="项目名称：">
                <el-select
                  class="selectBhz"
                  v-model="getList.fprojectid"
                  placeholder="所有项目"
                  @change="selectProject"
                >
                  <el-option
                    v-for="v in projectsObj"
                    :key="v.fItemName"
                    :label="v.fItemName"
                    :value="v.fItemValue"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="6"
              ><el-form-item label="标段名称：">
                <el-select
                  class="selectBhz"
                  v-model="getList.fCompanyid"
                  placeholder="所有标段"
                  @change="selectTender"
                >
                  <el-option
                    v-for="v in tendersObj"
                    :key="v.fItemName"
                    :label="v.fItemName"
                    :value="v.fItemValue"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="6"
              ><el-form-item label="拌和站：">
                <el-select
                  class="selectBhz"
                  v-model="getList.stationId"
                  placeholder="所有拌和站"
                  @change="selectStation"
                >
                  <el-option
                    v-for="v in bhzDataList"
                    :key="v.fItemName"
                    :label="v.fItemName"
                    :value="v.fItemValue"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="6">
              <el-form-item label="拌和机：">
                <el-select
                  class="selectBhz"
                  v-model="getList.machineId"
                  placeholder="所有拌和机"
                >
                  <el-option
                    v-for="v in bhjDataList"
                    :key="v.fItemName"
                    :label="v.fItemName"
                    :value="v.fItemValue"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="出料时间：">
                <el-date-picker
                  :picker-options="pickerOptions"
                  value-format="yyyy-MM-dd"
                  @change="selectTime"
                  v-model="timeDuring"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>

            <el-col
              :span="6"
              :offset="6"
              class="searchBtn"
              style="text-align: right; display: flex; justify-content: end"
            >
              <el-form-item>
                <el-button
                  type="primary"
                  :loading="editLoading"
                  @click="yj_searchForm(getList)"
                  >查询</el-button
                >
                <el-button @click="reset">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
  
      </div>

      <!-- 表格 -->
      <div class="text_table TensionExceedHandle">
        <div class="materialTable" style="height: 100px">
          <tableData
          :nodata="false"
          :issss="false"
            v-if="table_config.thead.length > 1"
            :config="table_config"
            :dataList="tableData"
            :loading="listLoading"
            @selection-change="selectionChange"
            @sort-change="sortChange"
          />
        </div>
        <div class="materialCharts">
          <myCharts
            :time1="this.timeDuring[0]"
            :time2="this.timeDuring[1]"
            :commonData="chartsData"
          >
          </myCharts>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import actions from "@/actions";
import TableData from "@/components/public/el-table"; //主页面列表
import myCharts from "@/components/waterManage/dataAnalysisManage/waterMaterialCharts";

import {
  getUserCompanyTreeList, //左侧机构树接口
  getWaterInfoMaterialUsed, //查询
  getStationmachineList, //拌合站列表
  getMachineListByStationId, //拌和机
} from "@/api/request.js";

export default {
  name: "waterMaterial",
  data() {
    return {
      name: "waterMaterial", //按钮配置页面代号
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },

      rowInfo: null, //当前行信息
      projectsObj: [], //项目名
      tendersObj: [],
      falgId: null,
      editLoading: false,
      timeDuring: [], //主页面日期筛选绑定值
      formData: {}, //新增编辑向后台传递对象
      bhzDataList: [], //拌合站
      bhjDataList: [], //拌合机
      value2: "", //主页面日期筛选绑定值

      // 列表配置项
      table_config: {
        imgKeyWord: "url",
        fileKeyWord: "url",
        fileName: "fFileName",
        dialogHeight: "",
        thead: [],
        isCheckBox: false,
        isIndex: false,
        isOperation: true,
        // 表格操作列
      },
      tableData: [], //表格列表数据列表
      chartsData: {}, //echarts数据
      highlightBd: true, //左侧机构树停留背景色

      treelength: false, //左侧树显示隐藏
      checked: true, //查看下级按钮
      showName: "未选择公司",

      fId: null, //点击当前列表数据fId
      listLoading: false, //列表数据加载loading
      // 列表给后端传参
      getList: {
        fprojectid: "",
        fCompanyid: "", // 施工标段id，不为空
        startDates: "", // 开始时间
        endDates: "", // 截至时间
        stationId: "", // 拌合站id
        machineId: "", // 拌合机id
        buildPart: "", // 施工部位
        page: "", // 当前页数
        pageSize: "", // 每页显示条数
        warningLevel: "",
        fCloseoperation: "",
      },
      companyTree: "", //所有数据
      treeData: [], //左侧树状图数据

      // 左侧机构树
      defaultProps: {
        children: "children",
        label: "fShortname",
      },
      checkData: [],
      userInfo: {}, //登录用户
      treeText: {
        //树状图参数
        companyId: null,
        userId: null,
        text: null,
      },
    };
  },
  components: {
    TableData, //主页面列表

    myCharts,
  },
  mounted() {
    actions.onGlobalStateChange((state, prev) => {
      this.userInfo = prev.currentUser;
      this.token = prev.token;
      var data = prev.currentUser.projects;
      this.projectsObj = [
        {
          fItemName: "所有项目",
          fItemValue: "",
        },
      ];
      data.map((item) => {
        this.projectsObj.push({
          fItemName: item.projectName,
          fItemValue: item.projectId,
        });
      });
    }, true);

    this.getDate();
    // this.btnRole(this.name);
  },

  methods: {
    selectProject(e) {
      this.falgId = e;
      this.tendersObj = [];
      this.bhzDataList = [];
      this.getList.fCompanyid = "";
      this.getList.stationId = "";
      this.getList.machineId = "";
      this.bhjDataList = [];
      // console.log(e);
      this.getTender(e);
    },
    async getTender(e) {
      await this.$http(
        " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
          e,
        {
          headers: {
            "Content-Type": "application/json",
            Authorization: localStorage.getItem("access_token"),
          },
        }
      ).then((res) => {
        if (res.status == 200) {
          var data = res.data.data.GXCompanyList;
          this.tendersObj = [];
          if (data.length == 0) {
                        this.$message.warning({
                            content: "有其他人员登录此账号！",
                            duration: 1000,
                        });
                        setTimeout(() => {
                            window.location.href = "http://172.18.5.201:1080/login";
                        }, 1200);
                    } else   if (data.length == 1) {
            data.map((item) => {
              this.tendersObj.push({
                fItemName: item.sectionName,
                fItemValue: item.sectionId,
              });
            });
            this.falgId = data[0].sectionId;

            this.getList.fCompanyid = data[0].sectionId;
          } else {
            this.tendersObj = [
              {
                fItemName: "所有标段",
                fItemValue: "",
              },
            ];
            data.map((item) => {
              this.tendersObj.push({
                fItemName: item.sectionName,
                fItemValue: item.sectionId,
              });
            });
          }
        }
      });
    },
    selectTender(e) {
      this.falgId = e;

      this.getList.stationId = "";

      this.getList.machineId = "";
      this.bhjDataList = [];
      this.bhzDataList = [];

      this.mixingPlantList(0, this.getList);
    },
    selectStation(e) {
      this.getList.machineId = "";
      this.bhjDataList = [];
      this.getbhjDataMethods(e, 4);
    },
    // 重置按钮
    reset() {
      this.timeDuring = [];
      this.getList = {
        fprojectid: "",
        fCompanyid: "", // 施工标段id，不为空
        startDates: "", // 开始时间
        endDates: "", // 截至时间
        stationId: "", // 拌合站id
        machineId: "", // 拌合机id
        buildPart: "", // 施工部位
        page: "", // 当前页数
        pageSize: "", // 每页显示条数
        warningLevel: "",
        fCloseoperation: "",
      };
    },
    selectBhzFun(e) {
      this.getList.machineId = "";
      this.getbhjDataMethods(e);
    },
    getbhjDataMethods(data) {
      getMachineListByStationId({ stationId: data })
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;
            this.bhjDataList = [
              {
                fItemName: "所有拌和机",
                fItemValue: "",
              },
            ];
            arr.map((item) => {
              this.bhjDataList.push({
                fItemName: item.fMachinename,
                fItemValue: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 获取列表数据
    yj_searchForm(data) {
      this.listLoading = true;
      if (this.getList.buildPart == "施工部位") {
        data.buildPart = "";
      }
      if (this.getList.machineId == "所有拌合机") {
        data.machineId = "";
      }
      if (this.getList.stationId == "所有拌合站") {
        data.stationId = "";
      }
      window.sessionStorage.setItem(
        "GxEmbedded_waterManage_getLists",
        JSON.stringify(data)
      );
      var id;
      if(data.fCompanyid!=''){
        id=data.fCompanyid
      }else {
        id=data.fprojectid
      }
      var sc = {
        fCompanyid: id, // 施工标段id，不为空
        startDates: data.startDates, // 开始时间
        endDates: data.endDates, // 截至时间
        stationId: data.stationId, // 拌合站id
        machineId: data.machineId, // 拌合机id
        buildPart: "", // 施工部位
        page: data.page, // 当前页数
        pageSize: data.pageSize, // 每页显示条数
      };
      getWaterInfoMaterialUsed(sc)
        .then((res) => {
          if (res.errcode == 0) {
           
            var data = null;
            var StrengthGrade = null; //X轴
            var ActualCalculation = []; // Y轴
            var DiskNumber = []; //次数
            var tableDataList = [{}];

            if (res.data.length == 0) {
              this.chartsData = {
                StrengthGrade: ["水", "水泥", "骨料"],
                ActualCalculation: [0, 0, 0],
                DiskNumber: DiskNumber,
              };
              this.table_config.thead = [
                {
                  label: "原材料	",
                  prop: "type",
                  minWidth: "100px",
                  align: "center",
                },
                {
                  label: "水	",
                  prop: "water",
                  minWidth: "100px",
                  align: "center",
                },
                {
                  label: "水泥	",
                  prop: "cement",
                  minWidth: "100px",
                  align: "center",
                },
                {
                  label: "骨料",
                  prop: "aggregate",
                  minWidth: "100px",
                  align: "center",
                },
              ];

              this.tableData = [
                {
                  type: "消耗量(t)",
                  water: 0,
                  cement: 0,
                  aggregate: 0,
                },
              ];
              // tableDataList[0].type = "消耗量(t)";
              // this.tableData = tableDataList;
              this.listLoading = false;
            } else {
              var that = this;
              that.listLoading = false;
              data = res.data;

              StrengthGrade = ["水", "水泥", "骨料"]; //X轴
              ActualCalculation = [
                data[0].waterUsed,
                data[0].cementUsed,
                data[0].aggregateUsed,
              ]; // Y轴

              // 图表数据
              this.chartsData = {
                StrengthGrade: StrengthGrade,
                ActualCalculation: ActualCalculation,
                DiskNumber: DiskNumber,
              };
              this.table_config.thead = [
                {
                  label: "原材料	",
                  prop: "type",
                  minWidth: "100px",
                  align: "center",
                },
              ];
              StrengthGrade.map((item, index) => {
                this.table_config.thead.push({
                  label: item,
                  prop: item,
                  minWidth: "100px",
                  align: "center",
                });
                tableDataList[0].type = "消耗量(t)";
                tableDataList[0][item] = ActualCalculation[index];
              });

              that.tableData = tableDataList;
            }

            console.log("this.chartsData", this.chartsData);
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log("列表", error.msg);
        });
    },

    // 获取拌合站数据
    mixingPlantList(num, datas) {
      var data = {
        companyId: datas.fCompanyid, // 施工标段id
        modelType: num, // 类型
        stationType: "2", // 拌合站类型 1砼，2水稳，3沥青
      };
      getStationmachineList(data)
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;

            this.bhzDataList = [
              {
                fItemName: "所有拌合站",
                fItemValue: "",
              },
            ];

            arr.map((item) => {
              this.bhzDataList.push({
                fItemName: item.fMachinename,
                fItemValue: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          this.$message.error(error.msg);
        });
    },

    selectTime(e) {
      if (e[0] != "" && e[1] != "") {
        this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
        this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
        this.xlsxName = e[0] + "至" + e[1] + "混凝土拌和站记录数据";
      } else {
        this.getList.startDates = ""; // 开始时间
        this.getList.endDates = ""; // 截至时间
        this.xlsxName = "混凝土拌和站记录数据";
      }
      // this.yj_searchForm(this.getList);
    },

    getDate() {
      if (JSON.parse(sessionStorage.getItem("GxEmbedded_waterManage_getLists"))) {
        var getListStorage = JSON.parse(
          sessionStorage.getItem("GxEmbedded_waterManage_getLists")
        );
        this.getList = getListStorage;
        if (getListStorage.fprojectid != "") {
          this.getTender(getListStorage.fprojectid);
          this.falgId = getListStorage.fprojectid;
        }
        if (
          getListStorage.fCompanyid != "" &&
          getListStorage.fCompanyid != null
        ) {
          this.mixingPlantList(0, getListStorage);
          this.falgId = getListStorage.fCompanyid;
        }

        if (
          getListStorage.stationId != "" &&
          getListStorage.stationId != null
        ) {
          this.getbhjDataMethods(getListStorage.stationId);
        }
        this.yj_searchForm(this.getList);
        this.timeDuring = [this.getList.startDates, this.getList.endDates];
        this.xlsxName =
          this.getList.startDates +
          "至" +
          this.getList.endDates +
          "水稳拌和站记录数据";
      } else {
        this.getList.startDates = this.common.getDay(-90) + " " + "00:00:00"; // 开始时间
        this.getList.endDates = this.common.getDay(0) + " " + "23:59:59"; // 截至时间
        this.getList.fprojectid = this.projectsObj[1].fItemValue;

        this.getTender(this.getList.fprojectid);
        this.xlsxName =
          this.common.getDay(-90) +
          "至" +
          this.common.getDay(0) +
          " " +
          "23:59:59" +
          "水稳拌和站记录数据";

        this.timeDuring = [
          this.common.getDay(-90) + " " + "00:00:00",
          this.common.getDay(0) + " " + "23:59:59",
        ];
        this.yj_searchForm(this.getList);
      }
      console.log(66, this.timeDuring);
    },

    // 多选方法
    selectionChange() {},
    // 排序方法
    sortChange() {},
  },
};
</script>

<style scoped>
.TensionExceedHandle {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.printTables {
  background: #fff;
  display: flex;
  justify-content: end;
  padding: 5px;
  margin-top: 10px;
}
.materialCharts {
  height: calc(100% - 110px);
  width: 100%;
}
</style>
